package com.gupaoedu.mybatis.data;

import com.gupaoedu.mybatis.beans.SCData;
import com.gupaoedu.mybatis.mapper.SCDataMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by James on 2017/4/4.
 * From 咕泡学院出品
 * 老师咨询 QQ 2904270631
 */
public class MailSender {

    private static SqlSession sqlSession = null;

    public static Session getSession() {
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.setProperty("mail.host", "smtp.126.com");
        props.setProperty("mail.user", "pigenqq@126.com");
        props.setProperty("mail.password", "pigenqqcom123");

        return Session.getDefaultInstance(props, null);
    }

    public static void send(String[] bccArray) throws Exception {
        Session mailSession = getSession();
        System.out.println("Sending mail...");
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();
        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("[" + new Random().nextInt(1000) + "]" + "Java架构师视频限量下载");
        message.setFrom(new InternetAddress("pigenqq@126.com"));
        message.setContent
                ("<img src=\"http://piccdn.vlocallife.com/mail/ke.png\">\n" +
                                "<img src=\"http://piccdn.vlocallife.com/mail/mail1.png\">\n" +
                                "<img src=\"http://piccdn.vlocallife.com/mail/mail2.png\">\n" +
                                "<img src=\"http://piccdn.vlocallife.com/mail/mail3.png\">\n",
                        "text/html;charset=UTF-8");
        for (String bcc : bccArray) {
            message.addRecipient(Message.RecipientType.BCC,
                    new InternetAddress(bcc));
        }
        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress("pigenqq@126.com"));

        transport.connect("smtp.126.com", "pigenqq@126.com", "pigenqqcom123");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public static void main(String[] args) throws Exception {
//        send("18701799107@163.com");
//        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            readAndSend();
        }
//        System.out.println("20个耗时"+ (System.currentTimeMillis() - start)+"ms");
    }

    public static void readAndSend() throws Exception {
        SCDataMapper mapper = getSqlSession().getMapper(SCDataMapper.class);
        List<SCData> users = mapper.selectBySent();
        List<String> bccs = new ArrayList<String>(45);
        for (SCData user : users) {
            if (StringUtils.isBlank(user.getMobile())) {
                continue;
            }
            String bcc = user.getMobile() + "@163.com";
            bccs.add(bcc);
        }
        if (bccs.size() > 0) {
            int ms = new Random().nextInt(1000);
            System.out.println("sleep " + ms + "ms后" + "发送" + bccs.size());
            Thread.sleep(ms);
            try {
                send(bccs.toArray(new String[]{}));
            } catch (Exception e) {
                System.out.println("=============错误\n" + bccs);
                e.printStackTrace();
            }
            List<Integer> ids = new ArrayList<Integer>();
            for (SCData user : users) {
                ids.add(user.getId());
            }
            mapper.updateSend(ids);
            getSqlSession().commit();
        }
    }

    public static SqlSession getSqlSession() throws FileNotFoundException {
        InputStream configFile = new FileInputStream("E:\\workspace\\code\\gupaoedu-mybatis\\src\\main\\java\\com\\gupaoedu\\mybatis\\data\\mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configFile);
        if (null == sqlSession) {
            sqlSession = sqlSessionFactory.openSession();
        }
        return sqlSession;
    }
}
